package com.cssw.fyzb.core.dao.acct;

import com.cssw.fyzb.core.model.dodb.*;
import com.ydn.dbframe.plugin.activerecord.Db;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: yangyiqun
 * @Date: 2020/7/10 17:33
 */
@Repository
public class OutAcctDaoImpl implements OutAcctDao {

    @Override
    public UserWechat getWechatById(Long userId) {
        return UserWechat.dao.findById(userId);
    }

    @Override
    public UserAli getAliById(Long userId) {
        return UserAli.dao.findById(userId);
    }

    @Override
    public UserBank getBankByUserId(Long userId) {
        String sql = "select * from t_user_bank where user_id = ? and default_flag = ?";
        return UserBank.dao.findFirst(sql, userId, UserBank.DEFUALT_FLAG_YES);
    }

    @Override
    public UserBank getBankById(Long bankId) {
        return UserBank.dao.findById(bankId);
    }

    @Override
    public UserBank getBankByIdAndCardNo(Long userId, String cardNo) {
        String sql = "select * from t_user_bank where user_id = ? and card_no = ?";
        return UserBank.dao.findFirst(sql, userId, cardNo);
    }

    @Override
    public List<BankCode> listBankCode() {
        String sql = "select * from d_bank_code";
        return BankCode.dao.find(sql);
    }

    @Override
    public List<BankCodeCh> listBankCodeCh() {
        return BankCodeCh.dao.findAll();
    }

    @Override
    public List<UserBank> listBankById(Long userId) {
        String sql = "select * from t_user_bank where user_id = ? order by default_flag desc, create_time desc ";
        return UserBank.dao.find(sql, userId);
    }

    @Override
    public void deleteWechat(Long userId) {
        UserWechat.dao.deleteById(userId);
    }

    @Override
    public void deleteAli(Long userId) {
        UserAli.dao.deleteById(userId);
    }

    @Override
    public void deleteBank(Long bankId) {
        UserBank.dao.deleteById(bankId);
    }

    @Override
    public void saveWechat(Long userId, String name, String wechatNo, String receQrCode) {
        UserWechat userWechat = new UserWechat();
        userWechat.setCreateTime(new Date());

        userWechat.setUserId(userId);
        userWechat.setRealName(name);
        userWechat.setWechatNo(wechatNo);
        userWechat.setReceQrCode(receQrCode);
        userWechat.setUpdateTime(new Date());

        userWechat.save();
    }

    @Override
    public void saveAli(Long userId, String name, String aliNo, String receQrCode) {
        UserAli userAli = new UserAli();

        userAli.setCreateTime(new Date());
        userAli.setUserId(userId);
        userAli.setRealName(name);
        userAli.setAliNo(aliNo);
        userAli.setReceQrCode(receQrCode);
        userAli.setUpdateTime(new Date());

        userAli.save();
    }

    @Override
    public void saveBank(Long userId, String bankName, String cardNo, String openBank, String name,
                         int defaultFlag, String province, String city, String bankCode) {
        UserBank userBank = new UserBank();
        userBank.setUserId(userId);
        userBank.setRealName(name);
        userBank.setBankName(bankName);
        userBank.setCardNo(cardNo);
        userBank.setOpenBank(openBank);
        userBank.setCreateTime(new Date());
        userBank.setProvince(province);
        userBank.setDefaultFlag(defaultFlag);
        userBank.setCity(city);
        userBank.setBankCode(bankCode);
        userBank.save();
    }

    @Override
    public void updateAli(Long userId, String name, String aliNo, String receQrCode) {
        UserAli userAli = new UserAli();

        userAli.setUserId(userId);
        userAli.setRealName(name);
        userAli.setAliNo(aliNo);
        userAli.setReceQrCode(receQrCode);
        userAli.setUpdateTime(new Date());

        userAli.update();
    }

    @Override
    public void updateWechat(Long userId, String name, String wechatNo, String receQrCode) {
        UserWechat userWechat = new UserWechat();
        userWechat.setUserId(userId);
        userWechat.setRealName(name);
        userWechat.setWechatNo(wechatNo);
        userWechat.setReceQrCode(receQrCode);
        userWechat.setUpdateTime(new Date());

        userWechat.update();
    }

    @Override
    public void updateBank(Long bankId, Long userId, String bankName, String cardNo, String openBank, String name, int defaultFlag, String province, String city, String bankCode) {
        UserBank userBank = new UserBank();
        userBank.setBankId(bankId);
        userBank.setUserId(userId);
        userBank.setRealName(name);
        userBank.setBankName(bankName);
        userBank.setCardNo(cardNo);
        userBank.setOpenBank(openBank);
        userBank.setDefaultFlag(defaultFlag);
        userBank.setProvince(province);
        userBank.setCity(city);
        userBank.setBankCode(bankCode);
        userBank.update();
    }

    @Override
    public void updateBank(UserBank userBank) {
        userBank.update();
    }

    @Override
    public void defaultFlg(Long bankId) {
        StringBuilder str = new StringBuilder();
        str.append(" update t_user_bank set default_flag=? where bank_id=?");
        Db.update(str.toString(), UserBank.DEFUALT_FLAG_YES, bankId);
    }

    @Override
    public void noDefaultFlg(Long bankId) {
        StringBuilder str = new StringBuilder();
        str.append(" update t_user_bank set default_flag=? where bank_id=?");
        Db.update(str.toString(), UserBank.DEFUALT_FLAG_NO, bankId);
    }

    @Override
    public List<BankDistrict> listBankDistrict() {
        return BankDistrict.dao.findAll();
    }

    @Override
    public void updateNoDefaultFlgByUserId(Long userId) {
        StringBuilder str = new StringBuilder();
        str.append(" update t_user_bank set default_flag=? where user_id=?");
        Db.update(str.toString(), UserBank.DEFUALT_FLAG_NO, userId);
    }
}
